package com.hyzx.qbasic.admin.domain.service.impl;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.po.SysParamPO;
import com.hyzx.qbasic.admin.model.dto.KeyValuePair;
import com.hyzx.qbasic.admin.model.vo.req.admin.AddParamReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.DeleteParamReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.EditParamReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.GetParamListReq;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.common.util.OperationResUtils;
import com.hyzx.qbasic.admin.domain.repository.ISysParamRepository;
import com.hyzx.qbasic.admin.domain.service.ISysParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by 黄江华 on 2018/7/20
 */

@Service
public class SysParamServiceImpl implements ISysParamService {
    @Autowired
    private ISysParamRepository paramRepository;

    @Override
    public PageInfo<SysParamPO> getPagedParamList(GetParamListReq req) {
        PageInfo<SysParamPO> paramList = paramRepository.getPagedParamList(
                req.getPageNum(), req.getPageSize(), req.getpType());

        return paramList;
    }

    @Override
    public BaseOperationRes addParam(AddParamReq req) {
        SysParamPO paramPO = new SysParamPO();
        paramPO.setpType(req.getpType());
        paramPO.setpKey(req.getpKey());
        paramPO.setpValue(req.getpValue());
        paramPO.setSort(req.getSort());
        paramPO.setRemark(req.getRemark());
        paramPO.setCreateTime(new Date());
        paramPO.setUpdateTime(new Date());

        boolean success = paramRepository.addParam(paramPO);
        if (!success)
            return OperationResUtils.fail("新增数据字典失败");

        return OperationResUtils.success();
    }

    @Override
    public BaseOperationRes deleteParam(DeleteParamReq req) {
        boolean success = paramRepository.deleteParam(req.getpIdList());
        if (!success)
            return OperationResUtils.fail("删除数据字典失败");

        return OperationResUtils.success();
    }

    @Override
    public SysParamPO getParamInfo(int pId) {
        return paramRepository.getParamInfo(pId);
    }

    @Override
    public BaseOperationRes editParam(EditParamReq req) {
        //检查数据字典是否存在
        SysParamPO existedParam = paramRepository.getParamInfo(req.getpId());
        if (existedParam == null)
            return OperationResUtils.fail("数据字典不存在");

        SysParamPO paramPO = new SysParamPO();
        paramPO.setpId(req.getpId());
        paramPO.setpType(req.getpType());
        paramPO.setpKey(req.getpKey());
        paramPO.setpValue(req.getpValue());
        paramPO.setSort(req.getSort());
        paramPO.setRemark(req.getRemark());
        paramPO.setUpdateTime(new Date());

        boolean success = paramRepository.editParam(paramPO);
        if (!success)
            return OperationResUtils.fail("编辑数据字典失败");

        return OperationResUtils.success();
    }

    @Override
    public List<KeyValuePair> getParamListByType(String type) {
        return paramRepository.getParamListByType(type);
    }
}
